package com.fumin.service;

import com.fumin.beans.PageQuery;
import com.fumin.beans.PageResult;
import com.fumin.common.RequestHolder;
import com.fumin.dao.SysAclMapper;
import com.fumin.exception.ParamException;
import com.fumin.model.SysAcl;
import com.fumin.model.SysUser;
import com.fumin.param.AclParam;
import com.fumin.util.BeanValidator;
import com.fumin.util.IpUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  权限下面的权限模块点
 * @Author xiayuxuanmin
 * @Date 2019/11/8
 * @Description
 */
@Service
public class SysAclService {
    @Resource
    private SysAclMapper sysAclMapper;
    @Resource
    private SysLogService sysLogService;

    public void save(AclParam param){
        BeanValidator.check(param);
        if (checkExist(param.getAclModuleId(),param.getName(),param.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限模块点!");
        }
        SysAcl sysAcl = SysAcl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
                .type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark())
                .build();
        sysAcl.setCode(this.generateCode());
        sysAcl.setOperator(RequestHolder.getCurrentUser().getUsername());//TODO:
        sysAcl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));//TODO:
        sysAcl.setOperateTime(new Date());
        sysAclMapper.insertSelective(sysAcl);
        sysLogService.saveAclLog(null,sysAcl);
    }

    public void update(AclParam param){
        BeanValidator.check(param);
        if (checkExist(param.getAclModuleId(),param.getName(),param.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限模块点!");
        }
        SysAcl before = sysAclMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待更新的权限点不存在!");
        SysAcl after = SysAcl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
                .type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark())
                .build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());//TODO:
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));//TODO:
        after.setOperateTime(new Date());
        sysAclMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveAclLog(before,after);
    }

    public boolean checkExist(int aclModuleId,String name,Integer id){
        return sysAclMapper.countByNameAndAclModuleId(aclModuleId,name,id) > 0;
    }

    /**
     * 生成随机的code字符串,
     * 其中随机int数为1~100
     * @return
     */
    public String generateCode(){
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date())+"_"+(int)(Math.random()*100);
    }

    /**
     * 根据分页对象查找分页数据
     * @param aclModuleId
     * @param page
     * @return
     */
    public PageResult<SysAcl> getPageByAclModuleId(int aclModuleId, PageQuery page) {
        BeanValidator.check(page);
        int count = sysAclMapper.countByAclModule(aclModuleId);
        if (count > 0) {
            List<SysAcl> list = sysAclMapper.getPageByAclModuleId(aclModuleId, page);
            return PageResult.<SysAcl>builder().total(count).data(list).build();
        }
        return PageResult.<SysAcl>builder().build();
    }
}
